<template>
  <div>
    <span @click="openDialog" style="margin-right: 10px; color: #ff4040; cursor: pointer">举报</span>
    <el-dialog title="举报" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item label="举报原因" prop="description">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" prefix-icon="el-icon-edit"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="report">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { userReport } from "@/api/post/report";

export default {
  name: "ReportButton",
  props: {
    postId: Number
  },
  data() {
    const validateReason = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入举报原因"));
      } else {
        callback();
      }
    };
    return {
      user: {},
      dialogVisible: false,
      form: {
        reason: ''
      },
      rules: {
        reason: [
          { required: true, trigger: "blur", validator: validateReason }
        ]
      }
    }
  },
  methods: {
    openDialog() {
      this.dialogVisible = true
    },
    report() {
      this.$refs.form.validate(valid => {
        if (valid) {
          userReport({
            "postId": Number(this.postId),
            "fromUserId": Number(this.user.id),
            "reason": this.form.reason
          }).then(res => {
            if (res.data.code === 200) {
              this.dialogVisible = false
              this.$message.success(res.data.message)
            } else {
              // this.$message.error(res.data.message)
            }
          })
        } else {
          return false;
        }
      })
    },
  },
  created() {
    this.user = JSON.parse(Cookies.get("user"))
    this.getPostInfo()
  }
}
</script>
